<meta charset='utf8'>
<?php 
/****
布尔教育 高端PHP培训
培  训: http://www.itbool.com
论  坛: http://www.zixue.it
****/

abstract class aUpload {
public $allowExt = array('jpg' , 'jpeg' , 'png' , 'rar');
public $maxSize = 1; // ๋य़Ӥփय़ੜ,զMԅ֖ܔ
protected $error = ''; // Კ᧏௳מ
/**
* $_ຉړFILESӾ$nameऒጱ$_ྲֺ,௳מFILESӾጱ['pic']
* @param string $name ᤒܔӾfileᤒܔᶱጱname؀
* @return array Ӥփ෈կጱ,௳מ۱(ތtmp_name,oname[ӧݸތᖗጱ෈կݷᑍ] ,
ext[ݸᖗ],size)
*/
abstract public function getInfo($name);
/**
* ڠୌፓ୯ ࣁ୮ڹᗑᒊጱ໑ፓ୯ጱuploadፓ୯Ӿ,ೲଙ/์෭ ڠୌፓ୯
* @return string ፓ୯᪠ஆ ֺ /upload/2015/0331
*/
abstract public function createDir();
/**
* ኞ౮ᵋ๢෈կݷ
* @param int $len ᵋ๢ਁᒧԀጱᳩଶ
* @return string ೰ਧᳩଶጱᵋ๢ਁᒧԀ
*/
abstract public function randStr($len = 8);
/**
* Ӥփ෈կ
* @param string $name ᤒܔӾfileᤒܔᶱጱname؀
* @return string Ӥփ෈կጱ᪠ஆ,՗web໑ፓ୯୏তᦇ,ই/upload/2015/0331/a
.jpg
*/
abstract public function up($name);
/*
ڣෙ $_FILES[$name]
᧣አgetInfo ຉ෈ړկጱय़ੜ,ݸᖗᒵ
᧣አcheckType
᧣አcheckSize
᧣አcreateDir
᧣አrandStrኞ౮ᵋ๢෈կݷ
ᑏۖ,ᬬࢧ᪠ஆ
*/
/**
* ༄ၥ෈կጱᔄ,ࣳইرݝᦜjpg,jpeg,png,rar,ӧرᦜexe
* @param $ext ෈կጱݸᖗ
* @return boolean
*/
abstract protected function checkType($ext);
/**
* ༄ၥ෈կጱय़ੜ
* @param $size ෈կጱय़ੜ
* @return boolean
*/
abstract protected function checkSize($size);
/**
௳מᲙ᧏ݐ᧛ *
*/
public function getError() {
return $this->error;
}
}

class Upload extends aUpload{
	public function getInfo($name){
		return $_FILES[$name];
	}

	public function createDir(){
		$dir = 'up/'.date('Y/m',time());
		if (!is_dir($dir)) {
			mkdir($dir,0777,true);
		}
		return $dir;
	}

	public function randStr($len = 8){
		return md5(time()).mt_rand(111,999);
	}

	protected function checkType($ext){
		return in_array($ext,$this->allowExt);
	}

	protected function checkSize($size){
		$ss = $size< $this-> maxSize * 1024 *1024;
	}

	public function up($name){
		if(!isset($_FILES[$name])) {
			echo '别瞎闹，给文件';exit();
		}
		$info = $this->getInfo($name);
		$e = ltrim(strrchr($info['name'],'.'),'.');
		if(!$this->checkType($e)){
			echo '给图片';exit();
		}

		if($this->checkSize($info['size'])){
			echo '我不喜欢大的';exit();
		}

		$dir = $this->createDir();
		$filename = $this->randStr().'.'.$e;

		if(move_uploaded_file($info['tmp_name'],$dir.'/'.$filename)){
			$data['path'] = $dir;
			$data['filename'] = $filename;
			return $data;
		}
	}
}

$file = new Upload();
$a = $file->up('pic');
var_dump($a);

?>